<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" fullscreen center>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-row class="bg-row">
        <el-col :span="8">
          <el-form-item label="名称" prop="name">
            <el-input v-model="dataForm.name" placeholder="名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="编码" prop="code">
            <el-input v-model="dataForm.code" placeholder="编码"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8"></el-col>
      </el-row>
      <el-row class="bg-row">
        <el-form-item label="商品类别" prop="ctgIdList">
          <el-checkbox-group v-model="dataForm.ctgIdList">
            <el-checkbox v-for="ctg in ctgList" :label="ctg.ctgId" :key="ctg.ctgId">{{ctg.ctgName}}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </el-row>
      <el-row class="bg-row">
        <el-col :span="8">
          <el-form-item label="联系人" prop="contact">
            <el-input v-model="dataForm.contact" placeholder="联系人"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="电话号码" prop="phone">
            <el-input v-model="dataForm.phone" placeholder="电话号码"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="传真" prop="fax">
            <el-input v-model="dataForm.fax" placeholder="传真"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row class="bg-row">
        <el-form-item label="地址" prop="address">
          <el-input v-model="dataForm.address" placeholder="地址"></el-input>
        </el-form-item>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit" :loading="ischeck" v-prevent-re-click>确定</el-button>
    </span>
  </el-dialog>
</template>
<script>
    import {getSupplyObj, addSupplyObj, putSupplyObj, updateSupplyObj} from '@/api/eims/supply'
    import {getCtgList} from '@/api/eims/ctg'
    export default {
    data () {
      return {
				ischeck: false,
        visible: false,
        ctgList: [],
        dataForm: {
          id: 0,
          name: '',
          code: '',
          ctgIdList: [],
          address: '',
          contact: '',
          phone: '',
          fax: ''
        },
        dataRule: {
          name: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          code: [
            { required: true, message: '编码不能为空', trigger: 'blur' }
          ],
          // ctgId: [
          //   { required: true, message: '商品类别id不能为空', trigger: 'blur' }
          // ],
          address: [
            { required: true, message: '地址不能为空', trigger: 'blur' }
          ],
          contact: [
            { required: true, message: '联系人不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '电话号码不能为空', trigger: 'blur' }
          ],
          fax: [
            { required: true, message: '传真不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    mounted () {
      this.dataForm.ctgIdList = []
      getCtgList().then(response => {
        if(response.data.code == '0'){
          const ctgList = response.data.data
          ctgList.forEach((item,index) => {
            let ctgObj = {ctgId: item.id,ctgName:item.name}
            this.ctgList.push(ctgObj)
          })
        }else{
          this.$message.error(response.data.msg)
        }
      })
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          let _ctgIdList = []
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getSupplyObj(this.dataForm.id).then(response => {
              if(response.data.code == '0'){
                let list = response.data.data.list
                list.forEach((item) => _ctgIdList.push(item.ctgId))
                response.data.data.ctgIdList = _ctgIdList
                this.dataForm = response.data.data
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
				this.ischeck = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              // 更新修改供应商详细信息
              updateSupplyObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('修改成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
                this.ischeck = false
              }).catch(() => {
                this.ischeck = false
              })
            } else {
              addSupplyObj(this.dataForm).then(response => {
                if(response.data.code == '0'){
                  this.$message.success('添加成功')
                  this.visible = false
                  this.$emit('refreshDataList')
                }else{
                  this.$message.error(response.data.msg)
                }
                this.ischeck = false
              }).catch(() => {
                this.ischeck = false
              })
            }
          }else{
            this.ischeck = false
          }
        })
      }
    }
  }
</script>
